﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using MySql.Data.MySqlClient;
using LLKanWebService.Model;
using MySql.Data.Types;
namespace LLKanWebService.BLL
{
    public class ScoreManager : MySQLHepler
    {
        UserManager usm = new UserManager();
        public List<Score> findAll()
        {

            openConn();
            MySqlCommand cmd = new MySqlCommand("select * from score order by play_score");
            cmd.Connection = connection;
            MySqlDataReader reader = cmd.ExecuteReader();
            List<Score> scorelist = new List<Score>();
            while (reader.Read())
            {
                Score score = new Score();
                score.ID = reader.GetInt32("id");
                score.PlayScore = reader.GetInt32("play_score");
                score.PlayTime = DateTime.Parse(reader.GetMySqlDateTime("play_time").ToString());
                score.User = usm.findUserById(reader.GetInt32("user"));
                scorelist.Add(score);
            }
            reader.Close();
            closeConn();
            return scorelist;
        }
        public Score findScoreById(int userid)
        {
            openConn();
            MySqlCommand cmd = new MySqlCommand("select * from score where user = ?userid");
            cmd.Connection = connection;
            cmd.Parameters.AddWithValue("userid", userid);
            MySqlDataReader reader = cmd.ExecuteReader();
            Score score = null;
            if (reader.Read())
            {
                score = new Score();
                score.ID = reader.GetInt32("id");
                score.PlayScore = reader.GetInt32("play_score");
                score.PlayTime = DateTime.Parse(reader.GetMySqlDateTime("play_time").ToString());
                score.User = usm.findUserById(reader.GetInt32("user"));
            }
            reader.Close();
            closeConn();
            return score;
        }
        public bool changeScore(int userid, int playscore)
        {
            Score score = findScoreById(userid);
            string sqlstr = "";
            MySqlCommand cmd = null;
            openConn();
            //判断null不行
            if (score != null)
            {
                //修改
                if (score.PlayScore < playscore)
                {
                    sqlstr = "update score set play_score = ?playscore,play_time = now() where id = ?id";
                    cmd = new MySqlCommand(sqlstr);
                    cmd.Connection = connection;
                    cmd.Parameters.AddWithValue("playscore", playscore);
                    cmd.Parameters.AddWithValue("id", score.ID);
                }
            }
            else
            {
                //新加
                sqlstr = "insert into score(play_score,user,play_time) values(?playscore,?userid, now())";
                cmd = new MySqlCommand(sqlstr);
                cmd.Connection = connection;
                cmd.Parameters.AddWithValue("playscore", playscore);
                cmd.Parameters.AddWithValue("userid", userid);
            }
            int count = 0;
            if (cmd != null)
            {
                count = cmd.ExecuteNonQuery();
            }
            closeConn();
            if (count > 0)
            {
                return true;
            }

            return false;
        }

    }
}